using System;
using Gtk;
using JmShared;
using MessageType = Gtk.MessageType;

namespace JiveMessengerLinux
{
    public partial class Options : Gtk.Window
    {
        private string oldNickname { get; set; }
        public Options() : base(Gtk.WindowType.Toplevel)
        {
            this.Build();
           
            this.checkButtonAutostart.Active = StaticSettings.SettingStartWithWindows;
            this.entryNickname.Text = Client.CurrentUserInfo.NickName;
            this.oldNickname = this.entryNickname.Text;
            this.checkbuttonAutoSignin.Active = StaticSettings.AutoLogin;
            this.checkbuttonStartMinimized.Active = StaticSettings.SettingStartMinimized;
            this.checkbuttonEnableDebugLog.Active = StaticSettings.SettingEnableDebugLogging;
            this.checkbuttonStoreOfflineMessages.Active = StaticSettings.SettingStoreMessagesWhileOffline;
            this.entryOfflineMessageAgeLimit.Text = StaticSettings.SettingOfflineMessagesAgeLimit.ToString();
            this.entryOfflineMessageCount.Text = StaticSettings.SettingOfflineMessagesCountLimit.ToString();
            this.entryOfflineMessageAgeLimit.Sensitive = this.entryOfflineMessageCount.Sensitive = checkbuttonStoreOfflineMessages.Active;
            TreeIter ti;
            this.comboboxLogVerbosity.Model.GetIterFirst(out ti);
            do
            {
                if ((string)this.comboboxLogVerbosity.Model.GetValue(ti, 0) == StaticSettings.SettingDebugLogLevel /*The value I look for*/)
                {
                    this.comboboxLogVerbosity.SetActiveIter(ti);
                    break;
            }

            }while (this.comboboxLogVerbosity.Model.IterNext(ref ti));

                //This loop doesn't check if a value has been found so I will check if the last TreeIter has the value I look for
            if ((string)this.comboboxLogVerbosity.Model.GetValue(ti,0)!=StaticSettings.SettingDebugLogLevel/*The value I look for*/ )
            {
                //Haven't found the value in the TreeStore
                
            } 
        }

        protected void OnBnSaveClicked(object sender, EventArgs e)
        {
            Client.CurrentUserInfo.NickName = this.entryNickname.Text;
            if (Client.CurrentUserInfo.NickName != oldNickname)
            {
                Client.CurrentUserInfo.NickName = Client.CurrentUserInfo.NickName;
                this.Title = "Jive Messenger" + " - " + (String.IsNullOrEmpty(Client.CurrentUserInfo.NickName) ?
                                                             Client.CurrentUserInfo.UserName : Client.CurrentUserInfo.NickName + " <" + Client.CurrentUserInfo.UserName + ">");
                ChatMessage msg = new ChatMessage
                    {
                        Sender = Client.CurrentUserInfo,
                        MessageType = JmShared.MessageType.UpdateUserInfo
                    };
                //client.Write(ChatSocketService.SerializeMessage(msg));
                Client.SendMessage(msg);
            }
            StaticSettings.SettingStartWithWindows = this.checkButtonAutostart.Active;
            Client.CurrentUserInfo.NickName=this.entryNickname.Text;
            StaticSettings.AutoLogin = this.checkbuttonAutoSignin.Active;
            StaticSettings.SettingStartMinimized = this.checkbuttonStartMinimized.Active;
            StaticSettings.SettingEnableDebugLogging = this.checkbuttonEnableDebugLog.Active;
            StaticSettings.SettingDebugLogLevel = this.comboboxLogVerbosity.ActiveText;
            StaticSettings.SettingStoreMessagesWhileOffline = this.checkbuttonStoreOfflineMessages.Active;
            int age;
            if (Int32.TryParse(this.entryOfflineMessageAgeLimit.Text, out age))
            {
                if (age > 24)
                    age = 25;
                StaticSettings.SettingOfflineMessagesAgeLimit = age;
            }

            Int32 count;
            if (Int32.TryParse(this.entryOfflineMessageCount.Text, out count))
            {
                if (count > 250)
                    count = 250;
                StaticSettings.SettingOfflineMessagesCountLimit = count;
            }
            this.Destroy();
        }

        protected void OnCheckbuttonStoreOfflineMessagesToggled(object sender, EventArgs e)
        {
            CheckButton cb = sender as CheckButton;

                this.entryOfflineMessageAgeLimit.Sensitive =this.entryOfflineMessageCount.Sensitive =cb.Active;

           
        }
    }
}

